<?php
/**
 *  商户分类管理
 */
namespace Merch\Controller;

use Common\Controller\AdminbaseController;

class AdminCategoryController extends AdminbaseController {

    protected $category_model;
    protected $taxonomys=array("merch"=>"商户","floor"=>"楼层");

    function _initialize() {
        parent::_initialize();
        $this->category_model = D("Merch/MerchCategory");
        $this->assign("taxonomys",$this->taxonomys);
    }


    public function index(){
        $result = $this->category_model->order(array("listorder"=>"asc","taxonomy"=>"asc"))->select();
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $r) {
            //$r['str_manage'] = '<a href="' . U("AdminCategory/add", array("parent" => $r['cate_id'])) . '">'.L('ADD_SUB_CATEGORY').'</a> | <a href="' . U("AdminCategory/edit", array("id" => $r['cate_id'])) . '">'.L('EDIT').'</a> | <a class="js-ajax-delete" href="' . U("AdminCategory/delete", array("id" => $r['cate_id'])) . '">'.L('DELETE').'</a> ';
            $r['str_manage'] = ' <a href="' . U("AdminCategory/edit", array("id" => $r['cate_id'])) . '">'.L('EDIT').'</a> | <a class="js-ajax-delete" href="' . U("AdminCategory/delete", array("id" => $r['cate_id'])) . '">'.L('DELETE').'</a> ';
            // $url=U('portal/list/index',array('id'=>$r['cate_id']));
            // $r['url'] = $url;
            $r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
            $r['id']=$r['cate_id'];
            $r['parentid']=$r['parent'];

            if($r['istop'] == 1){
                $str = '<i class="fa fa-heart"></i>';
            }else{
                $str = '';
            }

            $r['istops'] = $str;

            $array[] = $r;


        }

        $tree->init($array);
        $str = "<tr>
                    <td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
                    <td>\$id</td>
                    <td>\$spacer <a href='\$url' target='_blank'>\$name\$istops</a></td>
                    <td>\$taxonomys</td>
                    <td>\$str_manage</td>
                </tr>";
        $taxonomys = $tree->get_tree(0, $str);
        $this->assign("taxonomys", $taxonomys);
        $this->display();
    }

    // 商户分类添加
    public function add(){
        $parentid = I("get.parent",0,'intval');
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $terms = $this->category_model->order(array("path"=>"asc"))->select();

        $new_terms=array();
        foreach ($terms as $r) {
            $r['id']=$r['cate_id'];
            $r['parentid']=$r['parent'];
            $r['selected']= (!empty($parentid) && $r['cate_id']==$parentid)? "selected":"";
            $new_terms[] = $r;
        }
        $tree->init($new_terms);
        $tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
        $tree=$tree->get_tree(0,$tree_tpl);

        $this->assign("terms_tree",$tree);
        $this->assign("parent",$parentid);
        $this->display();
    }

    // 商户分类添加提交
    public function add_post(){
        if (IS_POST) {
            $category=I();
            $category['smeta']=json_encode($_POST['smeta']);

            $result=$this->category_model->add($category);
            if ($result!==false) {
                $this->success("添加成功！",U("AdminCategory/index"));
            } else {
                $this->error("修改失败！");
            }
            // if ($this->category_model->create()!==false) {
            //     if ($this->category_model->add()!==false) {
            //         F('all_terms',null);
            //         $this->success("添加成功！",U("AdminCategory/index"));
            //     } else {
            //         $this->error("添加失败！");
            //     }
            // } else {
            //     $this->error($this->category_model->getError());
            // }
        }
    }

    // 商户分类编辑
    public function edit(){
        $id = I("get.id",0,'intval');
        $data=$this->category_model->where(array("cate_id" => $id))->find();

        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $terms = $this->category_model->where(array("cate_id" => array("NEQ",$id), "path"=>array("notlike","%-$id-%")))->order(array("path"=>"asc"))->select();
        // print_r($terms);
        $new_terms=array();
        foreach ($terms as $r) {
            $r['id']=$r['cate_id'];
            $r['parentid']=$r['parent'];
            $r['selected']=$data['parent']==$r['cate_id']?"selected":"";
            $new_terms[] = $r;
        }

        $tree->init($new_terms);
        $tree_tpl="<option value='\$id' \$selected>\$spacer\$name</option>";
        $tree=$tree->get_tree(0,$tree_tpl);

        $this->assign("terms_tree",$tree);
        // print_r($data);
        $this->assign("data",$data);
        $this->assign("smeta",json_decode($data['smeta'],true));
        $this->display();
    }

    // 商户分类编辑提交
    public function edit_post(){
        if (IS_POST) {
            $category=I();
            $category['smeta']=json_encode($_POST['smeta']);
            $result=$this->category_model->save($category);
            if ($result!==false) {
                $this->success("修改成功！",U("AdminCategory/index"));
            } else {
                $this->error("修改失败！");
            }
        }
    }

    // 商户分类排序
    public function listorders() {
        $status = parent::_listorders($this->category_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    // 删除商户分类
    public function delete() {
        $id = I("get.id",0,'intval');
        $count = $this->category_model->where(array("parent" => $id))->count();

        if ($count > 0) {
            $this->error("该菜单下还有子类，无法删除！");
        }

        if ($this->category_model->delete($id)!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

}